<template>
  <!-- 查看员工组件 -->
  <div class="details-container">
    <el-card>
      <el-tabs v-model="active">
        <el-tab-pane label="登陆账户设置" name="login">
          <el-form ref="form" label-width="120px" style="margin-left: 120px; margin-top:30px" :model="userObj" :rules="rules">
            <el-form-item label="姓名:" prop="username">
              <el-input v-model="userObj.username" style="width:300px" />
            </el-form-item>
            <el-form-item label="密码:" prop="newPassword">
              <el-input v-model="userObj.newPassword" style="width:300px" type="password" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="uploadInfo">更新</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="user">
          <UserInfo />
        </el-tab-pane>
        <el-tab-pane label="岗位信息" name="work">
          <JobInfo />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getUserDetails } from '@/api/user'
import { keepEmployeesInfo } from '@/api/employees'
import UserInfo from './userInfo.vue'
import JobInfo from './JobInfo.vue'
export default {
  name: 'EmployeesDetails',
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      active: 'login',
      userObj: {
        username: '',
        newPassword: ''
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空' }
        ],
        newPassword: [
          { required: true, message: '新密码不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度为6-16位' }
        ]
      }
    }
  },
  computed: {
    id() {
      return this.$route.params.id
    }
  },
  created() {
    this.getEmployeesInfo()
  },
  methods: {
    async getEmployeesInfo() {
      const data = await getUserDetails(this.id)
      // console.log(data)
      this.userObj = data
    },
    async uploadInfo() {
      try {
        await this.$refs.form.validate()
      } catch {
        return
      }
      await keepEmployeesInfo({
        id: this.id,
        username: this.userObj.username,
        password: this.userObj.newPassword
      })
      this.$message.success('修改成功')
    }
  }
}
</script>

<style lang="scss" scoped>
.details {
  &-container {
    margin: 30px;
  }
}
</style>
